<?php

namespace app\queue\stomp;

use Webman\Stomp\RetryAbleConsumer;
use app\queue\BaseConsumer;
use Throwable;

/**
 * 发送审核反馈消息
 */
class SendReviewFeedbackMessage extends BaseConsumer implements RetryAbleConsumer
{

    // 要消费的队列名
    public $queue = 'send_review_feedback_message';

    // 连接名，对应 config/redis_queue.php 里的连接`
    public $connection = 'default';

    /**
     * 消费
     * @param $data
     * @throws Throwable
     */
    public function doConsume($data)
    {
        try {
            APP_DEBUG && trace(var_export($data, true), 'DEBUG');
            $userId = $data['user_id'];
            $sender = $data['sender'];

            $chat = teamones_request('teamones-im', 'chat/get_one_to_one_chat', ['param' => ['receiver' => $userId, 'sender' => $sender]])['data'];
            $chatGlobalId = $chat["global_id"];
            $type = $data["type"] ?? 'normal';
            $content = $data['content'];
            $contentType = $data['content_type'];

            $messageData = [
                "chat_global_id" => $chatGlobalId,
                'type' => $type,
                'content' => $content,
                'content_type' => $contentType,
            ];
            $q = teamones_request('teamones-im', 'message/send_message', ['data' => $messageData]);
        } catch (Throwable $e) {
            trace(format_exception_info($e), "ERR");
            throw $e;
        }
    }

    public function getTtr()
    {
        return 5;
    }

    public function canRetry($attempt, $error)
    {
        return $attempt <= 3;
    }
}
